import ajax from './ajax.js'

const ul = document.querySelector('ul');
const h1 = document.querySelector('h1');

//获取当前登录的用户名称
ajax.get('http://localhost:65535/getUserName',(value) => {
    h1.innerHTML = `欢迎${value}来到主页`;
});

//渲染当前用户的列表
function render(){
    ajax.get('http://localhost:65535/getList',(value) => {
        const data = JSON.parse(value);
        let str = ``;
        data.forEach((item,index) => {
            str += `<li id="${item._id}">${item.name}&emsp;&emsp;&emsp;${item.description}&emsp;&emsp;&emsp;${item.profession}&emsp;&emsp;&emsp;
                ${item.price}&emsp;&emsp;&emsp;
                <a href="http://localhost:65535/edit?id=${item._id}" class="a1">编辑</a>&emsp;
                <a href="#" class="a2">删除</a></li>`
        });
        ul.innerHTML = str;
    });
}
render();

//删除业务
ul.onclick = (event) => {
    if(event.target.innerHTML === '删除'){
        event.preventDefault()
        console.log('??');
        if(confirm('确认要删除吗？')){
            ajax.get('http://localhost:65535/del',(value) => {
                //更新主页数据
                render();
            },{id:event.target.parentNode.id});
        }
    }
}
